package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.*;
import com.tanhua.dubbo.api.TestSoulApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class TestSoulApiImpl implements TestSoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<PaperType>  findPaper() {
        return mongoTemplate.findAll(PaperType.class);
    }

    @Override
    public List<QuestionList> findQuestionByPid(String paperId) {
        Query query = new Query(Criteria.where("pid").is(paperId));
        List<QuestionList> questionLists = mongoTemplate.find(query, QuestionList.class);
        return questionLists;
    }

    @Override
    public List<OptionList> findOptionByPidAndQid(String paperId, String qid) {
        Query query = new Query(Criteria.where("pid").is(paperId).and("qid").is(qid));
        return mongoTemplate.find(query,OptionList.class);
    }

    @Override
    public SoulReport findReportByUserIdAndPid(Long userId, String i) {
        Query query = new Query(Criteria.where("userId").is(userId.toString()).and("paperId").is(i));
        return mongoTemplate.findOne(query,SoulReport.class);
    }

    @Override
    public Integer findOptionScore(String questionId, String optionId) {
        Query query = new Query(Criteria.where("qid").is(questionId).and("oid").is(optionId));
        return mongoTemplate.findOne(query, OptionList.class).getScore();
    }

    @Override
    public String findConclusionByReportType(Integer reportType) {
        Query query = new Query(Criteria.where("reportType").is(reportType));
        return mongoTemplate.findOne(query, ReportType.class).getConclusion();
    }

    @Override
    public String findCoverByReportType(Integer reportType) {
        Query query = new Query(Criteria.where("reportType").is(reportType));
        return mongoTemplate.findOne(query, ReportType.class).getCover();
    }

    @Override
    public void saveReport(SoulReport soulReport) {
        mongoTemplate.save(soulReport);
    }

    @Override
    public SoulReport findReportByReportId(String reportId) {
        Query query = new Query(Criteria.where("reportId").is(Integer.valueOf(reportId)));
        return mongoTemplate.findOne(query,SoulReport.class);
    }

    @Override
    public List<SoulReport> findReportByReportType(Integer reportType) {
        Query query = new Query(Criteria.where("reportType").is(reportType));
        return mongoTemplate.find(query,SoulReport.class);
    }

    @Override
    public void saveQuestion(QuestionList questionList) {
        mongoTemplate.save(questionList);
    }

    @Override
    public void saveOption(OptionList optionList) {
        mongoTemplate.save(optionList);
    }

    @Override
    public QuestionList findQuestionById(String s) {
        Query query = new Query(Criteria.where("pid").is(s));
        return mongoTemplate.findOne(query,QuestionList.class);
    }

    @Override
    public void saveReportType(ReportType reportType) {
        mongoTemplate.save(reportType);
    }
}
